package com.an;

public class HomeWork_12 {
    public static void main(String[] args) {
        int arr[]={10,9,8,7,6,5,5,4,2,12,31};
        AAA.bubble(arr);
        AAA.print(arr);
        System.out.println("");;
        System.out.println(AAA.find(arr,2));
        System.out.println(AAA.find(arr,7));
        System.out.println(AAA.find(arr,6));

    }
}

class AAA {
    public static void bubble(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tem = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tem;
                }
            }
        }
    }

    //打印数组
    public static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static int find(int[] arr, int k) {
        int left = 0;
        int right = arr.length - 1;
        int mid = (left + right) / 2;
        while (left<=right) {
            if(arr[mid]==k){
                return mid;
            }
            else if(arr[mid]>k){
                right=mid-1;
            } else if (arr[mid]<k) {
                left=mid+1;
            }
            mid=(left + right) / 2;
        }
        return -1;
    }
}